package fr.iut2.tc4.aideprojet2.torque.generated;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.torque.TorqueException;
import org.apache.torque.map.TableMap;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.util.Transaction;





/**
 * table notes
 *
 * This class was autogenerated by Torque on:
 *
 * [Mon Jan 21 09:20:16 CET 2013]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Notes
 */
public abstract class BaseNotes extends BaseObject
{
    /** Serial version */
    private static final long serialVersionUID = 1358756416467L;

    /** The Peer class */
    private static final NotesPeer peer =
        new NotesPeer();


    /** The value for the noteId field */
    private int noteId;

    /** The value for the matiereId field */
    private int matiereId;

    /** The value for the etudiantId field */
    private int etudiantId;

    /** The value for the note field */
    private double note;

    /** The value for the coef field */
    private int coef;


    /**
     * Get the NoteId
     *
     * @return int
     */
    public int getNoteId()
    {
        return noteId;
    }


    /**
     * Set the value of NoteId
     *
     * @param v new value
     */
    public void setNoteId(int v) 
    {

        if (this.noteId != v)
        {
            this.noteId = v;
            setModified(true);
        }


    }

    /**
     * Get the MatiereId
     *
     * @return int
     */
    public int getMatiereId()
    {
        return matiereId;
    }


    /**
     * Set the value of MatiereId
     *
     * @param v new value
     */
    public void setMatiereId(int v) throws TorqueException
    {

        if (this.matiereId != v)
        {
            this.matiereId = v;
            setModified(true);
        }


        if (aMatieres != null && !(aMatieres.getMatiereId() == v))
        {
            aMatieres = null;
        }

    }

    /**
     * Get the EtudiantId
     *
     * @return int
     */
    public int getEtudiantId()
    {
        return etudiantId;
    }


    /**
     * Set the value of EtudiantId
     *
     * @param v new value
     */
    public void setEtudiantId(int v) throws TorqueException
    {

        if (this.etudiantId != v)
        {
            this.etudiantId = v;
            setModified(true);
        }


        if (aEtudiant != null && !(aEtudiant.getEtudiantId() == v))
        {
            aEtudiant = null;
        }

    }

    /**
     * Get the Note
     *
     * @return double
     */
    public double getNote()
    {
        return note;
    }


    /**
     * Set the value of Note
     *
     * @param v new value
     */
    public void setNote(double v) 
    {

        if (this.note != v)
        {
            this.note = v;
            setModified(true);
        }


    }

    /**
     * Get the Coef
     *
     * @return int
     */
    public int getCoef()
    {
        return coef;
    }


    /**
     * Set the value of Coef
     *
     * @param v new value
     */
    public void setCoef(int v) 
    {

        if (this.coef != v)
        {
            this.coef = v;
            setModified(true);
        }


    }

    



    private Matieres aMatieres;

    /**
     * Declares an association between this object and a Matieres object
     *
     * @param v Matieres
     * @throws TorqueException
     */
    public void setMatieres(Matieres v) throws TorqueException
    {
        if (v == null)
        {
            setMatiereId( 0);
        }
        else
        {
            setMatiereId(v.getMatiereId());
        }
        aMatieres = v;
    }


    /**
     * Returns the associated Matieres object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Matieres object
     * @throws TorqueException
     */
    public Matieres getMatieres()
        throws TorqueException
    {
        if (aMatieres == null && (this.matiereId != 0))
        {
            aMatieres = BaseMatieresPeer.retrieveByPK(SimpleKey.keyFor(this.matiereId));
        }
        return aMatieres;
    }

    /**
     * Return the associated Matieres object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Matieres object
     * @throws TorqueException
     */
    public Matieres getMatieres(Connection connection)
        throws TorqueException
    {
        if (aMatieres == null && (this.matiereId != 0))
        {
            aMatieres = BaseMatieresPeer.retrieveByPK(SimpleKey.keyFor(this.matiereId), connection);
        }
        return aMatieres;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setMatieresKey(ObjectKey key) throws TorqueException
    {

        setMatiereId(((NumberKey) key).intValue());
    }




    private Etudiant aEtudiant;

    /**
     * Declares an association between this object and a Etudiant object
     *
     * @param v Etudiant
     * @throws TorqueException
     */
    public void setEtudiant(Etudiant v) throws TorqueException
    {
        if (v == null)
        {
            setEtudiantId( 0);
        }
        else
        {
            setEtudiantId(v.getEtudiantId());
        }
        aEtudiant = v;
    }


    /**
     * Returns the associated Etudiant object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Etudiant object
     * @throws TorqueException
     */
    public Etudiant getEtudiant()
        throws TorqueException
    {
        if (aEtudiant == null && (this.etudiantId != 0))
        {
            aEtudiant = BaseEtudiantPeer.retrieveByPK(SimpleKey.keyFor(this.etudiantId));
        }
        return aEtudiant;
    }

    /**
     * Return the associated Etudiant object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Etudiant object
     * @throws TorqueException
     */
    public Etudiant getEtudiant(Connection connection)
        throws TorqueException
    {
        if (aEtudiant == null && (this.etudiantId != 0))
        {
            aEtudiant = BaseEtudiantPeer.retrieveByPK(SimpleKey.keyFor(this.etudiantId), connection);
        }
        return aEtudiant;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setEtudiantKey(ObjectKey key) throws TorqueException
    {

        setEtudiantId(((NumberKey) key).intValue());
    }
   
        
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
            fieldNames.add("NoteId");
            fieldNames.add("MatiereId");
            fieldNames.add("EtudiantId");
            fieldNames.add("Note");
            fieldNames.add("Coef");
            fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by field (Java) name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    @Override
	public Object getByName(String name)
    {
        if (name.equals("NoteId"))
        {
            return new Integer(getNoteId());
        }
        if (name.equals("MatiereId"))
        {
            return new Integer(getMatiereId());
        }
        if (name.equals("EtudiantId"))
        {
            return new Integer(getEtudiantId());
        }
        if (name.equals("Note"))
        {
            return new Double(getNote());
        }
        if (name.equals("Coef"))
        {
            return new Integer(getCoef());
        }
        return null;
    }

    /**
     * Set a field in the object by field (Java) name.
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    @Override
	public boolean setByName(String name, Object value )
        throws TorqueException, IllegalArgumentException
    {
        if (name.equals("NoteId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setNoteId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("MatiereId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setMatiereId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("EtudiantId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setEtudiantId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("Note"))
        {
            if (value == null || ! (Double.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not a Double object.");
            }
            setNote(((Double) value).doubleValue());
            return true;
        }
        if (name.equals("Coef"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setCoef(((Integer) value).intValue());
            return true;
        }
        return false;
    }

    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    @Override
	public Object getByPeerName(String name)
    {
        if (name.equals(BaseNotesPeer.NOTE_ID))
        {
            return new Integer(getNoteId());
        }
        if (name.equals(BaseNotesPeer.MATIERE_ID))
        {
            return new Integer(getMatiereId());
        }
        if (name.equals(BaseNotesPeer.ETUDIANT_ID))
        {
            return new Integer(getEtudiantId());
        }
        if (name.equals(BaseNotesPeer.NOTE))
        {
            return new Double(getNote());
        }
        if (name.equals(BaseNotesPeer.COEF))
        {
            return new Integer(getCoef());
        }
        return null;
    }

    /**
     * Set field values by Peer Field Name
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    @Override
	public boolean setByPeerName(String name, Object value)
        throws TorqueException, IllegalArgumentException
    {
      if (BaseNotesPeer.NOTE_ID.equals(name))
        {
            return setByName("NoteId", value);
        }
      if (BaseNotesPeer.MATIERE_ID.equals(name))
        {
            return setByName("MatiereId", value);
        }
      if (BaseNotesPeer.ETUDIANT_ID.equals(name))
        {
            return setByName("EtudiantId", value);
        }
      if (BaseNotesPeer.NOTE.equals(name))
        {
            return setByName("Note", value);
        }
      if (BaseNotesPeer.COEF.equals(name))
        {
            return setByName("Coef", value);
        }
        return false;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    @Override
	public Object getByPosition(int pos)
    {
        if (pos == 0)
        {
            return new Integer(getNoteId());
        }
        if (pos == 1)
        {
            return new Integer(getMatiereId());
        }
        if (pos == 2)
        {
            return new Integer(getEtudiantId());
        }
        if (pos == 3)
        {
            return new Double(getNote());
        }
        if (pos == 4)
        {
            return new Integer(getCoef());
        }
        return null;
    }

    /**
     * Set field values by its position (zero based) in the XML schema.
     *
     * @param position The field position
     * @param value field value
     * @return True if value was set, false if not (invalid position / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    @Override
	public boolean setByPosition(int position, Object value)
        throws TorqueException, IllegalArgumentException
    {
    if (position == 0)
        {
            return setByName("NoteId", value);
        }
    if (position == 1)
        {
            return setByName("MatiereId", value);
        }
    if (position == 2)
        {
            return setByName("EtudiantId", value);
        }
    if (position == 3)
        {
            return setByName("Note", value);
        }
    if (position == 4)
        {
            return setByName("Coef", value);
        }
        return false;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    @Override
	public void save() throws Exception
    {
        save(BaseNotesPeer.DATABASE_NAME);
    }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
     *
     * @param dbName
     * @throws TorqueException
     */
    @Override
	public void save(String dbName) throws TorqueException
    {
        Connection con = null;
        try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
    }

    /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    @Override
	public void save(Connection con) throws TorqueException
    {
        if (!alreadyInSave)
        {
            alreadyInSave = true;



            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    BaseNotesPeer.doInsert((Notes) this, con);
                    setNew(false);
                }
                else
                {
                    BaseNotesPeer.doUpdate((Notes) this, con);
                }
            }

            alreadyInSave = false;
        }
    }


    /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param key noteId ObjectKey
     */
    @Override
	public void setPrimaryKey(ObjectKey key)
        
    {
        setNoteId(((NumberKey) key).intValue());
    }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    @Override
	public void setPrimaryKey(String key) 
    {
        setNoteId(Integer.parseInt(key));
    }


    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    @Override
	public ObjectKey getPrimaryKey()
    {
        return SimpleKey.keyFor(getNoteId());
    }
 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     */
    public Notes copy() throws TorqueException
    {
        return copy(true);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     *
     * @param con the database connection to read associated objects.
     */
    public Notes copy(Connection con) throws TorqueException
    {
        return copy(true, con);
    }

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     */
    public Notes copy(boolean deepcopy) throws TorqueException
    {
        return copyInto(new Notes(), deepcopy);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     * @param con the database connection to read associated objects.
     */
    public Notes copy(boolean deepcopy, Connection con) throws TorqueException
    {
        return copyInto(new Notes(), deepcopy, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     */
    protected Notes copyInto(Notes copyObj) throws TorqueException
    {
        return copyInto(copyObj, true);
    }

  
    /**
     * Fills the copyObj with the contents of this object using connection.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param con the database connection to read associated objects.
     */
    protected Notes copyInto(Notes copyObj, Connection con) throws TorqueException
    {
        return copyInto(copyObj, true, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     */
    protected Notes copyInto(Notes copyObj, boolean deepcopy) throws TorqueException
    {
        copyObj.setNoteId(noteId);
        copyObj.setMatiereId(matiereId);
        copyObj.setEtudiantId(etudiantId);
        copyObj.setNote(note);
        copyObj.setCoef(coef);

        copyObj.setNoteId( 0);

        if (deepcopy)
        {
        }
        return copyObj;
    }
        
    
    /**
     * Fills the copyObj with the contents of this object using connection.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     * @param con the database connection to read associated objects.
     */
    protected Notes copyInto(Notes copyObj, boolean deepcopy, Connection con) throws TorqueException
    {
        copyObj.setNoteId(noteId);
        copyObj.setMatiereId(matiereId);
        copyObj.setEtudiantId(etudiantId);
        copyObj.setNote(note);
        copyObj.setCoef(coef);

        copyObj.setNoteId( 0);

        if (deepcopy)
        {
        }
        return copyObj;
    }
    
    

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public NotesPeer getPeer()
    {
        return peer;
    }

    /**
     * Retrieves the TableMap object related to this Table data without
     * compiler warnings of using getPeer().getTableMap().
     *
     * @return The associated TableMap object.
     */
    @Override
	public TableMap getTableMap() throws TorqueException
    {
        return BaseNotesPeer.getTableMap();
    }


    @Override
	public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Notes:\n");
        str.append("NoteId = ")
           .append(getNoteId())
           .append("\n");
        str.append("MatiereId = ")
           .append(getMatiereId())
           .append("\n");
        str.append("EtudiantId = ")
           .append(getEtudiantId())
           .append("\n");
        str.append("Note = ")
           .append(getNote())
           .append("\n");
        str.append("Coef = ")
           .append(getCoef())
           .append("\n");
        return(str.toString());
    }
}
